package com.kscrm.web.controller.web;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import com.kscrm.common.utils.SecurityUtils;
import com.kscrm.web.criteria.ChannelCountCriteria;
import com.kscrm.web.criteria.ClientCountCriteria;
import com.kscrm.web.criteria.ProjectCountCriteria;
import com.kscrm.web.dto.AddChannelDto;
import com.kscrm.web.dto.AddNextChannelDto;
import com.kscrm.web.service.IClientService;
import com.kscrm.web.service.IProjectCollectionService;
import com.kscrm.web.service.IProjectService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.kscrm.common.annotation.Log;
import com.kscrm.common.core.controller.BaseController;
import com.kscrm.common.core.domain.AjaxResult;
import com.kscrm.common.enums.BusinessType;
import com.kscrm.web.domain.Channel;
import com.kscrm.web.service.IChannelService;
import com.kscrm.common.utils.poi.ExcelUtil;
import com.kscrm.common.core.page.TableDataInfo;

/**
 * 渠道Controller
 * 
 * @author Dhq
 * @date 2025-01-21
 */
@RestController
@RequestMapping("/web/channel")
public class ChannelController extends BaseController
{
    @Autowired
    private IChannelService channelService;
    @Autowired
    private IClientService clientService;
    @Autowired
    private IProjectService projectService;
    @Autowired
    private IProjectCollectionService projectCollectionService;

    /**
     * 查询渠道列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Channel channel)
    {
        startPage();
        Long channelId = SecurityUtils.getChannelId();
        if (channelId!= null) {
            channel.setParentId(channelId);
        }
        List<Channel> list = channelService.selectChannelList(channel);
        return getDataTable(list);
    }


    /**
     * 获取渠道详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(channelService.selectChannelById(id));
    }

    /**
     * 新增渠道
     */
    @Log(title = "新增渠道", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Valid AddChannelDto dto)
    {
        return toAjax(channelService.insertChannel(dto));
    }


    @PostMapping("/addNext")
    public AjaxResult addNext(@RequestBody @Valid AddNextChannelDto dto)
    {
        return toAjax(channelService.insertNextChannel(dto));
    }




    /**
     * 修改渠道
     */
    @Log(title = "更新渠道", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @Valid AddChannelDto dto)
    {
        return toAjax(channelService.updateChannel(dto));
    }

    /**
     * 更新数据
     * @param id
     * @return
     */
    @PutMapping("/updateNum/{id}")
    public AjaxResult updateNum(@PathVariable("id") Long id){
        // 统计更新
        int clientNum = clientService.selectClientCount(new ClientCountCriteria().setChannelId(id));
        int projectNum = projectService.selectProjectCount(new ProjectCountCriteria().setChannelId(id));
        int projectMoney = projectCollectionService.sumProjectMoney(new ProjectCountCriteria().setChannelId(id));
        return toAjax(channelService.updateChannelNumber(new Channel().setId(id)
                .setClientNum(clientNum)
                .setProjectNum(projectNum)
                .setMoneyNum(projectMoney)
                .setUpdateNumTime(new Date())));
    }


    /**
     * 删除渠道
     */
    @Log(title = "渠道", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(channelService.deleteChannelByIds(ids));
    }
}
